﻿Public MustInherit Class clsChart
    Private m_pGraph As Bitmap

    Private m_pBackgroundcolor As Color
    Private m_iColorCounter As Integer
    Private m_pChartColors As List(Of Color)

    Protected WithEvents m_pGraphEntries As clsGraphEntryCollection

    Public Event Graph_Refreshed(ByVal sender As clsChart)

    Public Sub New(ByVal dWidth As Integer, ByVal dHeight As Integer)
        m_pGraph = New Bitmap(dWidth, dHeight)

        m_pGraphEntries = New clsGraphEntryCollection
        m_pChartColors = clsGraphColors.ColorList
    End Sub

    Protected MustOverride Sub Render(ByRef g As Graphics)

    Public Property Backgroundcolor As Color
        Get
            Return m_pBackgroundcolor
        End Get
        Set(ByVal value As Color)
            m_pBackgroundcolor = value
        End Set
    End Property
    Public ReadOnly Property GraphImage As Bitmap
        Get
            Return m_pGraph
        End Get
    End Property

    Protected Function GetNextColor() As Color
        GetNextColor = m_pChartColors(m_iColorCounter)
        m_iColorCounter += 1
        Return GetNextColor
    End Function

    Public Property GraphEntries As clsGraphEntryCollection
        Get
            Return m_pGraphEntries
        End Get
        Set(ByVal value As clsGraphEntryCollection)
            m_pGraphEntries = value
        End Set
    End Property

    Private Sub Draw()
        ' Reset Colors
        m_iColorCounter = 0

        Dim g As Graphics = Graphics.FromImage(m_pGraph)

        g.Clear(m_pBackgroundcolor)

        ' Let the actual rendering be done by the inheriting child
        Render(g)

        g.Dispose()
        g = Nothing

        RaiseEvent Graph_Refreshed(Me)
    End Sub

    Private Sub m_pGraphEntries_Entries_Changed(ByVal sender As clsGraphEntryCollection) Handles m_pGraphEntries.Entries_Changed
        Draw()
    End Sub
End Class
